package com.exam.mihoyo;

import java.util.*;


public class Solution01 {
    public static void main(String[] args) {
        Solution01 instance = new Solution01();
        int[] temperatures = instance.temperatures(new int[]{3, 1, 4});
        for (int temperature : temperatures) {
            System.out.print(temperature + " ");
        }
    }
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 找出其中不含有重复字符的最长子串的长度
     * @param s string字符串 字符串
     * @return int整型
     */
    public int lengthOfLongestSubstring (String s) {
        HashMap<Character, Integer> map = new HashMap<>();
        int start = -1;
        int res = 0;
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(map.containsKey(ch)) {
                start = Math.max(start, map.get(ch));
            }
            res = Math.max(res, i - start);
            map.put(ch, i);
        }
        return res;
        // write code here
    }

    public int[] temperatures (int[] dailyTemperatures) {
        int len = dailyTemperatures.length;
        int[] res = new int[len];
        Deque<Integer> queue = new LinkedList<>();
        Deque<Integer> indexQueue = new LinkedList<>();
        for (int i = 0; i < len; i++) {
            while(!queue.isEmpty() && queue.peekLast() < dailyTemperatures[i]) {
                queue.pollLast();
                int index = indexQueue.pollLast();
                res[index] = i - index;
            }
            queue.offerLast(dailyTemperatures[i]);
            indexQueue.offerLast(i);
        }
        return res;
        // write code here
    }
}
